﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;
using System.Web.Security;

namespace outREACH
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            foreach (GridViewRow row in GridView1.Rows)
            {
                Label l0 = (Label)row.FindControl("Label53");
                Label l1 = (Label)row.FindControl("Label4");
                Label l2 = (Label)row.FindControl("Label5");
                Label l3 = (Label)row.FindControl("Label6");
                Label l4 = (Label)row.FindControl("Label7");
                Label l5 = (Label)row.FindControl("Label8");
                Label l6 = (Label)row.FindControl("Label9");
                Label l7 = (Label)row.FindControl("Label10");
                Label l8 = (Label)row.FindControl("Label11");

                if (l1.Text == "" || l2.Text == "" || l3.Text == "" || l5.Text == "")
                {
                    l1.Visible = false;
                    l2.Visible = false;
                    l3.Visible = false;                    
                    l4.Visible = false;                    
                    l5.Visible = false;                    
                    l6.Visible = false;                    
                    l7.Visible = false;
                    l8.Visible = false;
                    //l0.Visible = false;
                }

                Label l9 = (Label)row.FindControl("Label12");
                Label l10 = (Label)row.FindControl("Label13");
                Label l11 = (Label)row.FindControl("Label14");
                Label l12 = (Label)row.FindControl("Label15");
                Label l13 = (Label)row.FindControl("Label16");
                Label l14 = (Label)row.FindControl("Label17");
                Label l15 = (Label)row.FindControl("Label18");
                Label l16 = (Label)row.FindControl("Label19");

                if (l9.Text == "" || l10.Text == "" || l16.Text == "" || l12.Text == "")
                {
                    
                    l9.Visible = false;
                    l10.Visible = false;
                    l11.Visible = false;
                    l12.Visible = false;
                    l13.Visible = false;
                    l14.Visible = false;
                    l15.Visible = false;
                    l16.Visible = false;
                    //l0.Visible = false;
                }

                Label l17 = (Label)row.FindControl("Label20");
                Label l18 = (Label)row.FindControl("Label21");
                Label l19 = (Label)row.FindControl("Label23");
                Label l20 = (Label)row.FindControl("Label24");
                Label l21 = (Label)row.FindControl("Label25");
                Label l22 = (Label)row.FindControl("Label26");
                Label l23 = (Label)row.FindControl("Label27");
                Label l24 = (Label)row.FindControl("Label28");

                if (l17.Text == "" || l18.Text == "" || l21.Text == "" || l23.Text == "")
                {
                    l17.Visible = false;
                    l18.Visible = false;
                    l19.Visible = false;
                    l20.Visible = false;
                    l21.Visible = false;
                    l22.Visible = false;
                    l23.Visible = false;
                    l24.Visible = false;
                    l0.Visible = false;
                }
            }

            foreach (GridViewRow row in GridView2.Rows)
            {
                Label l25 = (Label)row.FindControl("Label29");
                Label l26 = (Label)row.FindControl("Label47");
                Label l27 = (Label)row.FindControl("Label48");
                Label l28 = (Label)row.FindControl("Label49");
                Label l29 = (Label)row.FindControl("Label50");
                Label l30 = (Label)row.FindControl("Label51");
                Label l31 = (Label)row.FindControl("Label52");
                Label l32 = (Label)row.FindControl("Label32");
                //Label l32 = (Label)row.FindControl("Label54");

                if (l25.Text == "" || l27.Text == ""|| l32.Text == "")
                {
                    l25.Visible = false;
                    l26.Visible = false;
                    l27.Visible = false;
                    l28.Visible = false;
                    l29.Visible = false;
                    l30.Visible = false;
                    l31.Visible = false;
                    l32.Visible = false;
                }

                Label l33 = (Label)row.FindControl("Label30");
                Label l34 = (Label)row.FindControl("Label33");
                Label l35 = (Label)row.FindControl("Label41");
                Label l36 = (Label)row.FindControl("Label42");
                Label l37 = (Label)row.FindControl("Label43");
                Label l38 = (Label)row.FindControl("Label44");
                Label l39 = (Label)row.FindControl("Label45");
                Label l40 = (Label)row.FindControl("Label46");

                if (l33.Text == "" || l34.Text == "" || l36.Text == "")
                {

                    l33.Visible = false;
                    l34.Visible = false;
                    l35.Visible = false;
                    l36.Visible = false;
                    l37.Visible = false;
                    l38.Visible = false;
                    l39.Visible = false;
                    l40.Visible = false;
                }
            }

            foreach (GridViewRow row in GridView3.Rows)
            {
                Label l41 = (Label)row.FindControl("Label54");
                Label l42 = (Label)row.FindControl("Label64");
                Label l43 = (Label)row.FindControl("Label65");
                Label l44 = (Label)row.FindControl("Label66");
                Label l45 = (Label)row.FindControl("Label67");
                Label l46 = (Label)row.FindControl("Label68");
                Label l47 = (Label)row.FindControl("Label56");
                Label l48 = (Label)row.FindControl("Label69");
                //Label l32 = (Label)row.FindControl("Label54");

                if (l41.Text == "" || l47.Text == "" || l43.Text == "" || l45.Text == "")
                {
                    l41.Visible = false;
                    l42.Visible = false;
                    l43.Visible = false;
                    l44.Visible = false;
                    l45.Visible = false;
                    l46.Visible = false;
                    l47.Visible = false;
                    l48.Visible = false;
                }

                Label l49 = (Label)row.FindControl("Label55");
                Label l50 = (Label)row.FindControl("Label57");
                Label l51 = (Label)row.FindControl("Label58");
                Label l52 = (Label)row.FindControl("Label59");
                Label l53 = (Label)row.FindControl("Label60");
                Label l54 = (Label)row.FindControl("Label61");
                Label l55 = (Label)row.FindControl("Label62");
                Label l56 = (Label)row.FindControl("Label63");

                if (l49.Text == "" || l50.Text == "" || l52.Text == "" || l54.Text == "")
                {

                    l49.Visible = false;
                    l50.Visible = false;
                    l51.Visible = false;
                    l52.Visible = false;
                    l53.Visible = false;
                    l54.Visible = false;
                    l55.Visible = false;
                    l56.Visible = false;
                }
            }

            foreach (GridViewRow row in GridView4.Rows)
            {
                Label l57 = (Label)row.FindControl("Label72");
                Label l58 = (Label)row.FindControl("Label75");
                Label l59 = (Label)row.FindControl("Label76");
                Label l60 = (Label)row.FindControl("Label77");
                Label l61 = (Label)row.FindControl("Label78");
                Label l62 = (Label)row.FindControl("Label79");
                Label l63 = (Label)row.FindControl("Label80");
                Label l64 = (Label)row.FindControl("Label81");
                Label l65 = (Label)row.FindControl("Label82");

                if (l58.Text == "" || l61.Text == "" || l63.Text == "" || l65.Text == "")
                {
                    l57.Visible = false;
                    l58.Visible = false;
                    l59.Visible = false;
                    l60.Visible = false;
                    l61.Visible = false;
                    l62.Visible = false;
                    l63.Visible = false;
                    l64.Visible = false;
                    l65.Visible = false;
                }
            }

            //if (Request.Cookies["myCookie"] != null)
            //{
            //    HttpCookie cookie = Request.Cookies.Get("myCookie");
            //    txtUser.Text = cookie.Values["username"];
            //    txtPass.Text = cookie.Values["password"];
            //    txtPass.Attributes.Add("value", cookie.Values["password"]);
            //    Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);
            //    Response.Cookies[txtPass.Text].Expires = DateTime.Now.AddDays(-1);
            //}

            

            if (!IsPostBack)
            {
                if (Request.Cookies["User"] != null)
                    txtUser.Text = Request.Cookies["User"].Value;
                if (Request.Cookies["Pass"] != null)
                    txtPass.Text = Request.Cookies["Pass"].Value;
                if (Request.Cookies["User"] != null && Request.Cookies["Pass"] != null)
                {
                    chkRemember.Checked = true;
                }
                if (Request.Cookies["User"] == null && Request.Cookies["Pass"] == null)
                {
                    chkRemember.Checked = false;
                }
            } 
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            if (chkRemember.Checked == true)
            {
                Response.Cookies["User"].Value = txtUser.Text;
                Response.Cookies["Pass"].Value = txtPass.Text;
                Response.Cookies["User"].Expires = DateTime.Now.AddMonths(2);
                Response.Cookies["Pass"].Expires = DateTime.Now.AddMonths(2);
            }
            else
            {
                Response.Cookies["User"].Expires = DateTime.Now.AddMonths(-1);
                Response.Cookies["Pass"].Expires = DateTime.Now.AddMonths(-1);
            }
            //HttpCookie myCookie = new HttpCookie("myCookie");
            //if (chkRemember.Checked == true)
            //{
            //    myCookie.Values.Add("username", txtUser.Text);
            //    myCookie.Values.Add("password", txtPass.Text);
            //    myCookie.Expires = DateTime.Now.AddDays(15);
            //    Response.Cookies.Add(myCookie);
            //}
            String hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPass.Text, "sha1");
            List<Users> results = Users.GetUser();
            foreach (Users user in results)
            {
                if (txtUser.Text.ToString() == user.userName.ToString())
                {
                    Users u = Users.AuthorizeUser(txtUser.Text);
                    string hashedPassworddata = FormsAuthentication.HashPasswordForStoringInConfigFile(u.password.ToString(), "sha1");
                    if (hashedPassword.ToString() == hashedPassworddata.ToString())
                    {
                        Session["UserName"] = u.userName.ToString();
                        Session["Country"] = u.country.ToString();
                        //Session["roles"] = u.role.ToString();
                        FormsAuthentication.RedirectFromLoginPage(u.userName.ToString(), false);
                        InvalidUP.Visible = false;
                        Response.Redirect("HomeMem.aspx");

                    }
                    else
                    {
                        InvalidUP.Visible = true;
                    }
                    break;
                }
            }
            

        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            GridView1.Visible = true;
            GridView2.Visible = false;
            GridView3.Visible = false;
            GridView4.Visible = false;
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            GridView1.Visible = false;
            GridView2.Visible = true;
            GridView3.Visible = false;
            GridView4.Visible = false;
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            GridView1.Visible = false;
            GridView2.Visible = false;
            GridView3.Visible = false;
            GridView4.Visible = true;
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            GridView1.Visible = false;
            GridView2.Visible = false;
            GridView3.Visible = true;
            GridView4.Visible = false;
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                Label l0 = (Label)row.FindControl("Label53");
                Label l1 = (Label)row.FindControl("Label4");
                Label l2 = (Label)row.FindControl("Label5");
                Label l3 = (Label)row.FindControl("Label6");
                Label l4 = (Label)row.FindControl("Label7");
                Label l5 = (Label)row.FindControl("Label8");
                Label l6 = (Label)row.FindControl("Label9");
                Label l7 = (Label)row.FindControl("Label10");
                Label l8 = (Label)row.FindControl("Label11");

                if (l1.Text == "" || l2.Text == "" || l3.Text == "" || l5.Text == "")
                {
                    l1.Visible = false;
                    l2.Visible = false;
                    l3.Visible = false;
                    l4.Visible = false;
                    l5.Visible = false;
                    l6.Visible = false;
                    l7.Visible = false;
                    l8.Visible = false;
                    //l0.Visible = false;
                }

                Label l9 = (Label)row.FindControl("Label12");
                Label l10 = (Label)row.FindControl("Label13");
                Label l11 = (Label)row.FindControl("Label14");
                Label l12 = (Label)row.FindControl("Label15");
                Label l13 = (Label)row.FindControl("Label16");
                Label l14 = (Label)row.FindControl("Label17");
                Label l15 = (Label)row.FindControl("Label18");
                Label l16 = (Label)row.FindControl("Label19");

                if (l9.Text == "" || l10.Text == "" || l16.Text == "" || l12.Text == "")
                {

                    l9.Visible = false;
                    l10.Visible = false;
                    l11.Visible = false;
                    l12.Visible = false;
                    l13.Visible = false;
                    l14.Visible = false;
                    l15.Visible = false;
                    l16.Visible = false;
                    //l0.Visible = false;
                }

                Label l17 = (Label)row.FindControl("Label20");
                Label l18 = (Label)row.FindControl("Label21");
                Label l19 = (Label)row.FindControl("Label23");
                Label l20 = (Label)row.FindControl("Label24");
                Label l21 = (Label)row.FindControl("Label25");
                Label l22 = (Label)row.FindControl("Label26");
                Label l23 = (Label)row.FindControl("Label27");
                Label l24 = (Label)row.FindControl("Label28");

                if (l17.Text == "" || l18.Text == "" || l21.Text == "" || l23.Text == "")
                {
                    l17.Visible = false;
                    l18.Visible = false;
                    l19.Visible = false;
                    l20.Visible = false;
                    l21.Visible = false;
                    l22.Visible = false;
                    l23.Visible = false;
                    l24.Visible = false;
                    l0.Visible = false;
                }
            }
        }
    }
}